<?php

namespace App\Http\Resources\OpenShop\ShopEngineeringConstruction;

use App\Enums\OpenShop\InvestmentPromotion\UploadFileResourceEnum;
use App\Http\Resources\BaseResource;
use App\Models\OpenShop\ShopEngineeringConstruction\OpenShopEngineeringConstructionModel;
use Illuminate\Http\Request;

class ShopEngineeringConstructionResource extends BaseResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  Request  $request
     * @return array
     */
    public function toArray($request): array
    {
        return array_merge([
            "id" => $this->id,
            "mainProcessId" => $this->mainProcessId,
            "orderNo" => $this->orderNo,
            "step" => $this->step,
            "finishStatus" =>  $this->finishStatus,
            "finishStatusText" =>  OpenShopEngineeringConstructionModel::FINISH_STATUS_MAP[$this->finishStatus] ?? "",
            "engineeringConstructionStartTime" => formatDateYmd($this->engineeringConstructionStartTime),
            "engineeringConstructionFinishTime" => formatDateYmd($this->engineeringConstructionFinishTime),
            "createdAt" => formatDate($this->createdAt->timestamp),
            "updatedAt" => formatDate($this->updatedAt->timestamp),
        ], $this->getAttachInfo());
    }

    /**
     * @return array
     */
    public function getAttachInfo(): array
    {
        return $this->attachRel->groupBy("position")->map(function($v, $k){
            return [
                "key" => UploadFileResourceEnum::ENGINEERING_CONSTRUCTION_MAP[$k],
                "url" => $v->pluck("url")->map("privateFilePath")->values()->toArray()
            ];
        })->values()->pluck("url", "key")->toArray();
    }
}
